<?php


   class UserAction extends CommonAction{
   
   
     public function tixian(){
	 
	  if(isset($_GET['keyword'])){
	  
	    session('keyword',$_GET['keyword']);
	  
	  }
	 
	 
	  include __ROOT__.'OAuth.php';
	  $info = M('wx')->where('id=2')->find();
	  $obj = new WechatApi($info['appid'],$info['appsecret']);//填你的appid,appsecret
	  $result = $obj->get_user_info();
	  
	  
	  
	  if($result){
	  
	      session('openid',$result['openid']);
	     //查询用户是否存在
	  
	      $user=M('dailiuser')->where('keyword="%s"',session('keyword'))->find();
		  
		  if($user==null){
		  
			exit('查无该用户');
		  
		  }
	  
	      $where['keyword']=array('eq',session('keyword'));
		  M('tixianzhong')->where($where)->delete();
	  
	      $arr['openid']=session('openid');
	      $arr['time']=time();
		  $arr['keyword']=session('keyword');
	 
	      M('tixianzhong')->add($arr);
		 
		  $this->sys=M('system')->where('id=1')->find();
		 
		  $this->user=$user;
		  $this->display();
	   
	   
	   }
	   
	 
	 }
	 public function savetixian(){
	 
	 
				//查询用户
				
				$user=M('dailiuser')->where('keyword="%s"',session('keyword'))->find();
				
				$mima=md5($_POST['mima']);
				
				if($mima!=$user['tixianpwd']){
				
				  $ajax['msg']=11;
				  
				  $this->ajaxReturn($ajax);
				
				}
				
				
				//提现金额小于2元断掉
				
				if($_POST['money']<2){
				  
				    $ajax['msg']=5;
				   
				   $this->ajaxReturn($ajax);
				  
				
				}
				
				
				
				//余额是否大于提现金额
				if($user['money']<$_POST['money']){
				  
				   $ajax['msg']=3;
				   
				   $this->ajaxReturn($ajax);
				
				}
				
				
				//查询出最后一次写入的openid 如果不一致 提示不能提现
				
				$tixianopenid=M('tixianzhong')->where('keyword="%s"',session('keyword'))->find();
				
				if(session('openid')!=$tixianopenid['openid']){
				
				    $ajax['msg']=4;
				    $this->ajaxReturn($ajax);
				}
				
				
				//首先扣除提现金额
				
				$st['money']=$user['money']-$_POST['money'];
				
				M('dailiuser')->where('keyword="%s"',session('keyword'))->save($st);
				
				
				
				
				
				//计算提成 扣除佣金
				
				$yongjin=M('yongjin')->where('id=1')->find();
				$sys=M('system')->where('id=1')->find();
				
				$kouchuyongjin=$sys['yongjin']/100;
				
				//dump($kouchuyongjin);
				
				$shijitixian=$_POST['money']-($_POST['money']*$kouchuyongjin);
				
				
				
				
			   //第一层上级返佣
				  if($user['upid']!=""){
				  
					   if($_POST['money']>=1){
				  
				  
						$top1=M('dailiuser')->where('id="%s"',$user['upid'])->find();
						
						
						//判断上级级别
						
						switch($top1['level']){
						
						  case "0":
						  
							$yongjin=$yongjin['yongjin'];
						  
						  break;
						  
						  case "1":
							
							$yongji=$yongjin['chujiyongjin'];
						  
						  break;
						  
						  case "2":
							
							$yongjin=$yongjin['gaojiyongjin'];
						  
						  break;
						
						
						}
						
						
					
						
						
						$yingfan=$_POST['money']*($yongjin/100);
						
						
						
						$yingfan=round($yingfan,2);
						
						
						
						$data['money']=$top1['money']+$yingfan;
						
					
						
						M('dailiuser')->where('id="%s"',$user['upid'])->save($data);
						
						
						//首先存入返佣表
						
					
						
						}
					
				  
					  }
							
				//返佣结束
				
				
				//纯利润
				
				$chunlirun=$_POST['money']*$kouchuyongjin-$yingfan;;
				
				
				
				
				
				
				 
				 $orderno="1234567890abcdefghigklim";
				 
				 $orderno=str_shuffle($order);
				 
				 $orderno=substr($order,0,6);
		
				
				
				vendor('Wxhongbao.hongbao');
				
				$wx = M('wx')->where('id=2')->find();
				header("Content-type: text/html; charset=utf-8");
		
				//APPID
				define('APPID',$wx['appid']);
				//商户id，10025915
				define('MCHID',$wx['shanghuhao']);
				//微信支付密钥，商户平台中，账户设置->安全设置->API安全->API密钥：api设置
				define('PARTNERKEY',$wx['miyao']);
		
				//以下接口均已调试成功，需要你自行对接到自己的系统中。
				//dump($tixian['money']);die;
		        // dump($shijitixian);
				
				$shijitixian=round($shijitixian,2);
				
				
				if($sys['isshenhe']==1){
				
				
				 $data['openid'] = session('openid');
				 $data['time']  = time();
				 $data['money'] = $shijitixian;
				 $data['username']=$user['username'];
				 $data['status']='2';
				 $data['tixianid']=$user['id'];
				 $data['shoufanyongid']=$user['upid'];
				 $data['yongjin']=$yingfan;
				 $data['maolirun']=$_POST['money']*$kouchuyongjin;
				 $data['chunlirun']=$chunlirun;
				 $data['openid']=session('openid');
				 
				  $re = M('tixian')->add($data);
				  
				  
				  $ajax['msg']="88";
				
				  $this->ajaxReturn($ajax);
				
				}
				
				
				
				//企业付款
				$obj1 = array();
				$obj1['openid']         	= session('openid');
				$obj1['amount']         	= $shijitixian*100;
				$obj1['desc']        		= "现金提现付款";
				$obj1['mch_appid']         	= APPID;
				$obj1['mchid']         		= MCHID;
				$obj1['partner_trade_no']	= MCHID.date('YmdHis').rand(1000, 9999);
				$obj1['spbill_create_ip']   = $_SERVER['REMOTE_ADDR'];
				$obj1['check_name']      	= "NO_CHECK";
				
		
				$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
				$wxHongBaoHelper = new WxPay();
				$result = $wxHongBaoHelper->pay($url, $obj1);
				$obj =  simplexml_load_string($result, null, LIBXML_NOCDATA); //转换成对象
			    //dump($obj);die;
				$arr  = $obj->result_code;//提取对象中某个属性值
				if($arr=="SUCCESS"){
				
				 //存入提现表
				
				 
				 $data['openid'] = session('openid');
				 $data['time']  = time();
				 $data['money'] = $shijitixian;
				 $data['username']=$user['username'];
				 $data['status']='1';
				 $data['tixianid']=$user['id'];
				 $data['shoufanyongid']=$user['upid'];
				 $data['yongjin']=$yingfan;
				 $data['maolirun']=$_POST['money']*$kouchuyongjin;
				 $data['chunlirun']=$chunlirun;
				  $data['openid']=session('openid');
				 $re = M('tixian')->add($data);
					            if($re){
								 $ajax['msg'] = '0';
								 $ajax['tishi']='您已经成功提现';
								 $this->ajaxReturn($ajax);
								}else{
								 $ajax['msg'] = '0';
								 $ajax['tishi']='成功提现写入数据失败';
								 $this->ajaxReturn($ajax);
								
								
								}
				}else{
						
						 $data['openid'] = session('openid');
						 $data['time']  = time();
						 $data['money'] =$shijitixian;
						 $data['username']=$user['username'];
						 $data['status']='2';
						 $data['tixianid']=$user['id'];
						 $data['shoufanyongid']=$user['upid'];
						 $data['yongjin']=$yingfan;
					     $data['maolirun']=$_POST['money']*$kouchuyongjin;
						 $data['chunlirun']=$chunlirun;
						 $re = M('tixian')->add($data);
						  $data['openid']=session('openid');
						  $ajax['msg'] =1;
						  $this->ajaxReturn($ajax);
							
			   }
				
			
		
		
	 }
	 
	 
	 public function jilu(){
	 
	   $myorder=M('orderinfo')->where('openid="%s"',session('openid'))->field('id')->select();
	   
	   foreach($myorder as $v){
	   
	      $arr[]=$v['id'];
	   
	   }
	   
	   $where['pid']=array('IN',$arr);
	   $where['openid']=array('eq',session('openid'));
	   $where['_logic'] = 'or';
	
	   $re=$this->page('buyer',10,'id desc',$where);
	   
	  //last('buyer');
	  
	  //计算收益
	  
	   $w['pid']=array('IN',$arr);
	   $w['status']=array('eq',1);
	   $s=M('buyer')->where($w)->select();
	   $shouyi=0;
	  foreach($s as $v){
	    
	    $shouyi+=$v['money'];
	  }
	  $this->shouyi=$shouyi;
	  
	  
	  $t['openid']=array('eq',session('openid'));
	  $t['status']=array('eq',1);
	  $t['type']=array('eq',1);
	  $tx=M('buyer')->where($t)->select();
	  $tixian=0;
	  
	  foreach($tx as $v){
	    
		$tixian+=$v['money'];
	  
	  }
	  $this->tixian=$tixian;
	  
	  
	  
	   
	   $this->re=$re;
	   
	   $this->display();
	 
	 }
   
   
   
   }